<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


if ( ! function_exists('ajax_image_upload'))
{
    function ajax_image_upload($selector, $options)
    {
        $config = array(
            'action' => '',
            'listElement' => null,  
            'template' => '<div class="qq-uploader">
                <div class="qq-upload-drop-area"><span>Drop files here to upload</span></div>
                <div class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">Upload a file</div>
                <ul class="qq-upload-list"></ul>
             </div>',
            'fileTemplate' => '<li>
                <span class="ui-icon ui-icon-carat-1-e qq-upload-bullet"></span>
                <span class="qq-upload-file"></span>
                <span class="qq-upload-spinner"></span>
                <span class="qq-upload-size"></span>
                <a class="qq-upload-cancel" href="#">Cancel</a>
                <span class="qq-upload-failed-text">Failed</span>
                <div class="clear"></div>
            </li>',
            'classes' => array(
                'button' => 'ui-button',
                'drop' => 'qq-upload-drop-area',
                'dropActive' => 'qq-upload-drop-area-active',
                'list' => 'qq-upload-list',
                'file' => 'qq-upload-file',
                'spinner' => 'qq-upload-spinner',
                'size' => 'qq-upload-size',
                'cancel' => 'qq-upload-cancel',
                'success' => 'qq-upload-success',
                'fail' => 'qq-upload-fail'
                )
        );        
        if(!is_array($options))
            $options = array('action' => $options);
        $options = array_merge($config, $options);        
        $json = json_encode($options);        
        $json = str_replace(",", ",\n",substr_replace($json, ', "element" : document.getElementById(\''.$selector.'\') }', strrpos($json, '}'))); 
        $return = "
        <script>
            $(document).ready(function(){
                var uploader = new qq.FileUploader(".$json.");
                $('#".$selector." .ui-button').hover(function(){
                    $(this).addClass('ui-state-hover');
                },function(){
                    $(this).removeClass('ui-state-hover');
                }).mousedown(function(){
                    $(this).addClass('ui-state-active');
                }).mouseup(function(){
                    $(this).removeClass('ui-state-active');
                });
                
           });
           </script>
        ";
        
        return $return;
        
        
    }
}